/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2019 HUST Dian Group
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Original Author: Pengyu Liu <eicliupengyu@gmail.com>
 * Modified by:     Zhichao Liu
 */
#include "ns3/assert.h"
#include "robo-geometry.h"
#include <cmath>

namespace ns3 {

FVector2
ConvCoordinate (FVector2 centerOfSubcoordinate, FAngle2 directionOfSubcoordinate,
                FVector2 coordinate)
{
  float cosOfAngle = cosf (directionOfSubcoordinate.m_phi);
  float sinOfAngle = sinf (directionOfSubcoordinate.m_phi);
  FVector2 offset (coordinate.m_x * cosOfAngle - coordinate.m_y * sinOfAngle,
                   coordinate.m_x * sinOfAngle + coordinate.m_y * cosOfAngle);
  return centerOfSubcoordinate + offset;
}

bool
IsCross (FVector2 st1, FVector2 ed1, FVector2 st2, FVector2 ed2)
{
  if ((st1.m_x > ed1.m_x ? st1.m_x : ed1.m_x) < (st2.m_x < ed2.m_x ? st2.m_x : ed2.m_x) ||
      (st1.m_y > ed1.m_y ? st1.m_y : ed1.m_y) < (st2.m_y < ed2.m_y ? st2.m_y : ed2.m_y) ||
      (st2.m_x > ed2.m_x ? st2.m_x : ed2.m_x) < (st1.m_x < ed1.m_x ? st1.m_x : ed1.m_x) ||
      (st2.m_y > ed2.m_y ? st2.m_y : ed2.m_y) < (st1.m_y < ed1.m_y ? st1.m_y : ed1.m_y))
    {
      return false;
    }
  if ((((st1.m_x - st2.m_x) * (ed2.m_y - st2.m_y) - (st1.m_y - st2.m_y) * (ed2.m_x - st2.m_x)) *
       ((ed1.m_x - st2.m_x) * (ed2.m_y - st2.m_y) - (ed1.m_y - st2.m_y) * (ed2.m_x - st2.m_x))) >
          0 ||
      (((st2.m_x - st1.m_x) * (ed1.m_y - st1.m_y) - (st2.m_y - st1.m_y) * (ed1.m_x - st1.m_x)) *
       ((ed2.m_x - st1.m_x) * (ed1.m_y - st1.m_y) - (ed2.m_y - st1.m_y) * (ed1.m_x - st1.m_x))) > 0)
    {
      return false;
    }
  return true;
}

bool
PointInTriangle (FVector2 point, FVector2 p1, FVector2 p2, FVector2 p3)
{
  FVector2 lineSeg1 = p1 - point;
  FVector2 lineSeg2 = p2 - point;
  FVector2 lineSeg3 = p3 - point;
  float cross12 = lineSeg1 * lineSeg2;
  float cross23 = lineSeg2 * lineSeg3;
  float cross31 = lineSeg3 * lineSeg1;
  return !(cross12 * cross23 < 0 || cross23 * cross31 < 0 || cross31 * cross12 < 0);
}

bool
CircleCrossLineSegment (FVector2 center, float redius, FVector2 st, FVector2 ed)
{
  if (center.GetDistance (st) <= redius || center.GetDistance (ed) <= redius)
    {
      return true;
    }
  float temp = center.GetDistance (st) * ed.GetDistance (st);
  NS_ASSERT_MSG (fabsf (temp) > EPS, "center.GetDistance (st) * ed.GetDistance (st), div by 0");
  float cos1 = Dot (center - st, ed - st) / temp;
  temp = center.GetDistance (ed) * ed.GetDistance (st);
  NS_ASSERT_MSG (fabsf (temp) > EPS, "center.GetDistance (ed) * ed.GetDistance (st), div by 0");
  float cos2 = Dot (center - ed, st - ed) / temp;

  if (cos1 <= 0 || cos2 <= 0)
    {
      return false;
    }
  return center.GetDistance (st) * sqrtf (1 - cos1 * cos1) <= redius;
}
} // namespace ns3
